// 01背包(记忆化搜索牛逼！)
/*
#include<iostream>
#include<cmath>
using namespace std;
const int N = 1010;
int w[N], v[N];
int dp[N][N];
int n, m;

int dfs(int u, int sum) {
    if (u > n)
        return 0;
    if (dp[u][sum] != -1)
        return dp[u][sum];
    int ans1 = dfs(u + 1, sum); //进入下次选择，不选择当前的物品
    int ans2 = 0;
    if (sum + v[u] <= m) {     //判断选择当前物品会不会超过体积，不超过则加上当前物品体积进入下次选择，同时注意加上价值
        ans2 = dfs(u + 1, sum + v[u]) + w[u];
    }

    return dp[u][sum]= max(ans1, ans2);

}



int main() {
    memset(dp, - 1,sizeof(dp));
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) 
        cin >> v[i] >> w[i];
    int ret = dfs(1, 0);
    cout << ret;

    return 0;
}
*/


//dp求解组合数
/*
#include <iostream>
using namespace std;

const int N = 1e9 + 7;
int a, b;
int dp[3010][3010];
int zuhu(int  x, int y) {
    if (dp[x][y] != -1)
        return dp[x][y];
    if (x == y)
        return 1;
    if (y == 0)
        return 1;

    return dp[x][y] = (zuhu(x - 1, y - 1) + zuhu(x - 1, y)) % N;

}

int main()
{
    memset(dp, -1, sizeof(dp));
    cin >> a >> b;
    int ret = zuhu(a, b);
    cout << ret;
    return 0;
}
*/

// 安全序列(晕)
/*
#include <iostream>
using namespace std;
const int M = 1e9 + 7;
const int N = 1e6 + 10;

int n, k;

int dp[N];
int dfs(int x) {
    if (x > n)
        return 1;
    if (dp[x] != -1)
        return dp[x];


    dp[x] = (dfs(x + 1) % M + dfs(x + k + 1) % M) % M;
    return dp[x];
}

int main()
{
    memset(dp, -1, sizeof(dp));
    cin >> n >> k;
    int ret = dfs(1);
    cout << ret;
    return 0;
}
*/

// 选数异或(dp)
/*
#include <iostream>
using namespace std;
const int M = 998244353;
const int N = 1e5 + 10;

int a[N];
int dp[N][65]; // 完整的二维数组

int main() {
    int n, x;
    cin >> n >> x;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }

    dp[0][0] = 1; // 初始条件：空子序列异或值为 0，方案数为 1。代表在前0个元素下，取值为0的个数为1.
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < 64; ++j) {
            dp[i][j] = (dp[i - 1][j] + dp[i - 1][j ^ a[i]]) % M; // 选择当前元素 a[i]
        }
    }

    cout << dp[n][x] << endl; // 输出最终结果
    return 0;
}
*/

// 选数异或(dfs)
/*
#include <iostream>
using namespace std;
const int M = 998244353;
const int N = 1e5 + 10;

int a[N];
int dp[N][65]; // 完整的二维数组
int n, x;
int dfs(int n, int x) {


    if (dp[n][x] != 0)
        return dp[n][x];
    if (n == 0 && x == 0)
        return 1;
    if (n == 0 && x != 0)
        return 0;
   
    dp[n][x] = (dfs(n - 1, x) + dfs(n - 1, x ^ a[n])) % M;
 
    return dp[n][x];

}


int main() {
    
    dp[0][0] = 1;
    cin >> n >> x;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    int ret = dfs(n, x);
    cout << ret << endl;
    return 0;
}
*/


//选数异或-2081
/*
#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#define ll long long


using namespace std;
int f[100005], pos[5000005];
int main() {
    int n, m, x; cin >> n >> m >> x;
    for (int i = 1; i <= n; ++i) {
        int a;
        scanf("%d", &a);
        f[i] = max(f[i - 1], pos[a ^ x]);
        pos[a] = i;
    }
    while (m--) {
        int l, r;
        scanf("%d%d", &l, &r);
        if (f[r] >= l)
            printf("yes\n");
        else
            printf("no\n");
    }
}
*/


// 动态规划01背包（DP）

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1010;
int v[N], w[N];
int dp[N][N];
void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> v[i] >> w[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            dp[i][j] = dp[i - 1][j]; // 不选
            if (j >= v[i]) {
                dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
                cout << " dp[" << i << "][" << j << "]:" << dp[i][j] << " ";
            }
        }
        cout << endl;
    }
    cout << dp[n][m] << endl;
}
int main() {
    int t = 1;
    for (int zu = 1; zu <= t; zu++) 
        solve();
    return 0;
}
